<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true">
      <el-form-item label="退库单编码">
        <el-input
          v-model="queryParams.refundNum"
          clearable
          style="width: 200px"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="退库机台">
        <el-input
          v-model="queryParams.machine_name"
          clearable
          style="width: 200px"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="退库日期">
        <el-date-picker
          v-model="queryParams.refundDate"
          style="width: 200px"
          type="date"
          placeholder="选择退库时间"
          format="YYYY-MM-DD"
          value-format="YYYY-MM-DD"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="Plus" @click="handleAdd"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button type="primary" plain icon="Download" @click="handleExport"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        v-model:showSearch="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table v-loading="loading" border :data="handleList">
      <el-table-column label="序号" width="60" type="index" align="center">
      </el-table-column>
      <el-table-column label="状态" align="center" prop="status">
        <template #default="scope">
          <dict-tag :options="storage_status" :value="scope.row.status" />
        </template>
      </el-table-column>
      <el-table-column label="退库单编码" align="center" prop="refundNum">
        <template v-slot="{ row }">
          <el-button type="primary" @click="handleView(row)" text>{{
            row.refundNum
          }}</el-button>
        </template>
      </el-table-column>
      <el-table-column label="退库时间" align="center" prop="refundDate" />
      <el-table-column label="退库机台" align="center" prop="machine_name" />

      <el-table-column label="创建人" align="center" prop="createBy" />
      <el-table-column label="创建时间" align="center" prop="createTime" />

      <el-table-column label="备注" align="center" prop="refundRemark" />

      <el-table-column
        label="操作"
        fixed="right"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template #default="scope">
          <el-tooltip
            content="编辑"
            placement="top"
            v-if="scope.row.status == 1"
          >
            <el-button
              link
              type="primary"
              icon="Edit"
              @click="handleEdit(scope.row)"
            ></el-button>
          </el-tooltip>
          <el-tooltip
            content="删除"
            placement="top"
            v-if="scope.row.status == 1"
          >
            <el-button
              link
              type="primary"
              icon="Delete"
              @click="handleDle(scope.row)"
            ></el-button>
          </el-tooltip>
          <el-button
            link
            v-if="scope.row.status == 1"
            type="primary"
            icon="Printer"
            @click="handleService(scope.row)"
            >提交</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <pagination
      style="margin-top: 0; padding-top: 0"
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script setup name="cancellation">
import {
  refundList,
  delRefund,
  updateRefund,
  exportRefund,
} from "@/api/storage/cancellation.js";
import useTagsViewStore from "@/store/modules/tagsView";
const tagsViewStore = useTagsViewStore();
const { proxy } = getCurrentInstance();

const { storage_status } = proxy.useDict("storage_status");
import { downLoad } from "@/utils/ruoyi";
import { ElMessage, ElMessageBox } from "element-plus";
const router = useRouter();
const route = useRoute();
const handleList = ref([]);
const loading = ref(false);
const total = ref(0);
const showSearch = ref(true);

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    code: null,
    handleDept: null,
  },
  rules: {
    code: [{ required: true, message: "处理单编码不能为空", trigger: "blur" }],
    handleDept: [
      { required: true, message: "处理单位不能为空", trigger: "blur" },
    ],
  },
});
const { queryParams, form, rules } = toRefs(data);
// 查看
if (route.query.code) {
  queryParams.value.code = route.query.code;
  refundList(queryParams.value).then((res) => {
    if (res.rows.length > 0) {
      tagsViewStore.delView(route);
      setTimeout(() => {
        handleView(res.rows[0]);
      }, 0);
    }
  });
}
const handleView = (row) => {
  router.push({
    path: "/storage/cancellation/details",
    query: {
      id: row.id,
      readonly: true,
      code: row.code,
      status: row.status,
    },
  });
};
// 新增
const handleAdd = () => {
  router.push("/storage/cancellation/details");
};
const handleEdit = (row) => {
  router.push({
    path: "/storage/cancellation/details",
    query: {
      id: row.id,
      code: row.code,
      status: row.status,
    },
  });
};
//提交
const handleService = (row) => {
  console.log("row", row);
  ElMessageBox.confirm(
    `确定要提交编码为 ${row.refundNum} 的处理单吗?`,
    "提示",
    {
      confirmButtonText: "确定",
      cancelButtonText: "取消",
      type: "warning",
    }
  ).then(() => {
    // 用户点击了确定按钮

    const formDate = { ...row };
    formDate.status = 2;
    updateRefund(formDate).then((response) => {
      if (response.code === 200) {
        ElMessage({
          message: "提交成功",
          type: "success",
        });
        getList();
      }
    });
  });
};
//删除
const handleDle = (row) => {
  ElMessageBox.confirm(
    "确定要删除编码为 " + row.refundNum + " 的退料单吗?",
    "提示",
    {
      confirmButtonText: "确定",
      cancelButtonText: "取消",
      type: "warning",
    }
  )
    .then(() => {
      // 用户点击了确定按钮
      delRefund(row.id).then((response) => {
        if (response.code === 200) {
          ElMessage({
            message: "删除成功",
            type: "success",
          });
          getList();
        }
      });
    })
    .catch(() => {
      //取消
    });
};

function getList() {
  loading.value = true;
  refundList(queryParams.value)
    .then((response) => {
      handleList.value = response.rows;
      total.value = response.total;
      loading.value = false;
    })
    .catch((err) => {
      loading.value = false;
    });
}
/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}
/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}
// 导出
const handleExport = () => {
  exportRefund(queryParams.value).then((res) => {
    downLoad(res.msg, "退库单列表");
  });
};
getList();
</script>
